package com.lyx.framework.utils;

import android.os.Environment;
import android.text.TextUtils;
import android.util.Log;

import com.lyx.framework.BuildConfig;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;
import java.text.SimpleDateFormat;
import java.util.Date;

public class LogUtils {

    private static SimpleDateFormat mSimpleDateFormat =
            new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    private static LogUtils mLogUtils;

    public final int DEBUG = 0;
    public final int INFO = 1;
    public final int ERROR = 2;
    public final int NOTHING = 3;

    public int level = BuildConfig.Level;

    private LogUtils() {

    }

    public static LogUtils getInstance() {
        if (mLogUtils == null) {
            synchronized (LogUtils.class) {
                if (mLogUtils == null) {
                    mLogUtils = new LogUtils();
                }
            }
        }
        return mLogUtils;
    }


    public void d(String text) {
        if (DEBUG >= level) {
            if (!TextUtils.isEmpty(text)) {
                Log.d(BuildConfig.LOG_TAG, text);
            }
        }
    }

    public void d(String tag, String text) {
        String logTag = tag;
        if (DEBUG >= level) {
            if (!TextUtils.isEmpty(text)) {
                if (TextUtils.isEmpty(tag)) {
                    logTag = BuildConfig.LOG_TAG;
                }
                Log.d(logTag, text);
            }
        }
    }

    public void i(String text) {
        if (INFO >= level) {
            if (!TextUtils.isEmpty(text)) {
                Log.d(BuildConfig.LOG_TAG, text);
            }
        }
    }

    public void i(String tag, String text) {
        String logTag = tag;
        if (INFO >= level) {
            if (!TextUtils.isEmpty(text)) {
                if (TextUtils.isEmpty(tag)) {
                    logTag = BuildConfig.LOG_TAG;
                }
                Log.d(logTag, text);
            }
        }
    }

    public void e(String text) {
        if (ERROR >= level) {
            if (!TextUtils.isEmpty(text)) {
                Log.d(BuildConfig.LOG_TAG, text);
            }
        }
    }

    public void e(String tag, String text) {
        String logTag = tag;
        if (ERROR >= level) {
            if (!TextUtils.isEmpty(text)) {
                if (TextUtils.isEmpty(tag)) {
                    logTag = BuildConfig.LOG_TAG;
                }
                Log.d(logTag, text);
            }
        }
    }

    /**
     * 写入内存卡
     */
//    public void writeToFile(String tag, String text) {
        //开始写入
//        FileOutputStream fileOutputStream = null;
//        BufferedWriter bufferedWriter = null;
//        try {
//            //文件路径
//            String fileRoot = Environment.getExternalStorageDirectory().getPath() + "/RainDrop/";
//            String fileName = "RainDrop.log";
//            // 时间 + 内容
//            String log = mSimpleDateFormat.format(new Date()) + " " + tag + " " + text + "\n";
//            //检查父路径
//            File fileGroup = new File(fileRoot);
//            //创建根布局
//            if (!fileGroup.exists()) {
//                fileGroup.mkdirs();
//            }
//            //创建文件
//            File fileChild = new File(fileRoot + fileName);
//            if (!fileChild.exists()) {
//                fileChild.createNewFile();
//            }
//            fileOutputStream = new FileOutputStream(fileRoot + fileName, true);
//            //编码问题 GBK 正确的存入中文
//            bufferedWriter = new BufferedWriter(new OutputStreamWriter(fileOutputStream, Charset.forName("gbk")));
//            bufferedWriter.write(log);
//        } catch (FileNotFoundException e) {
//            e.printStackTrace();
//            e(e.toString());
//        } catch (IOException e) {
//            e.printStackTrace();
//            e(e.toString());
//        } finally {
//            if (bufferedWriter != null) {
//                try {
//                    bufferedWriter.close();
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
//            }
//        }
//    }

}
